package com.pinc.poop;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.format.Time;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.ads.AdListener;

public class daily_poop extends PwdProtectWidgetActivity implements AdListener{
	private PincDate g_date;
	private ListView lv;
	private poop_adapter adpt;
	private DBClass dbHelper;
	private String queryStr;
	private LinearLayout editBar;
	private String editStr;
	private int editId;
	static final int TIME_DIALOG_ID = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.daily_poop);

		/* create SQLite Database class */
		dbHelper = new DBClass(this);

		/* get day from caller */
		Intent intent = this.getIntent();
		Bundle bd = intent.getExtras();
		g_date = new PincDate();
		g_date.parse(bd.getString("dd"));		
		queryStr = g_date.toDateStr();

		initWidget();
	}

	@Override
	protected void onValidResume() {
		update();
	}

	private void update()
	{
		adpt = new poop_adapter(this);
		Cursor cs = dbHelper.getRecord("date = ?", new String[]{queryStr}, "time");
		try{
			if(cs.moveToFirst())
			{
				do{
					poop_record pr = new poop_record();
					pr.timestr = cs.getString(cs.getColumnIndex("time"));
					pr.db_id = cs.getInt(cs.getColumnIndex("_id"));
					pr.color = cs.getInt(cs.getColumnIndex("color"));
					pr.shape = cs.getInt(cs.getColumnIndex("shape"));
					pr.amount = cs.getInt(cs.getColumnIndex("amount"));
					pr.memo = cs.getString(cs.getColumnIndex("memo"));
					adpt.add(pr);
				}while(cs.moveToNext());
			}
		} finally{
			cs.close();
		}
		cs.close();
		dbHelper.close();
		lv.setAdapter(adpt);

	}

	private class ItemClickListener implements OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,	long id) {
			TextView tv = (TextView) view.findViewById(R.id.textView_time);
			editStr = (String) tv.getText();
			editId = (int)id;
			editBar.setVisibility(View.VISIBLE);
		}
	}

	//	@Override
	//	protected Dialog onCreateDialog(int id) {
	//		switch (id) {
	//		case TIME_DIALOG_ID:
	//			return new TimePickerDialog(this, mTimeSetListener, 0, 0, true);
	//		}
	//		return null;
	//	}
	//
	//	@Override
	//	protected void onPrepareDialog(int id, Dialog dialog) {
	//		switch (id) {
	//		case TIME_DIALOG_ID:
	//			String hm[] = editStr.split(":");
	//			int mHour = Integer.parseInt(hm[0]);
	//			int mMinute = Integer.parseInt(hm[1]);
	//			((TimePickerDialog)dialog).updateTime(mHour, mMinute);
	//		}
	//	}
	//
	//	private TimePickerDialog.OnTimeSetListener mTimeSetListener =
	//		new TimePickerDialog.OnTimeSetListener() {
	//		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
	//			String hm[] = editStr.split(":");
	//			int mHour = Integer.parseInt(hm[0]);
	//			int mMinute = Integer.parseInt(hm[1]);
	//			if ( mHour == hourOfDay && mMinute == minute)
	//				return;
	//			Cursor cs = dbHelper.getRecord("date = ? AND time = ?", new String[]{g_date.toDateStr(), String.format("%02d:%02d", hourOfDay, minute)}, null);
	//			if ( cs.getCount() > 0)
	//			{
	//				cs.close();
	//				return;
	//			}
	//			cs.close();
	//			dbHelper.updateRecord(g_date.toDateStr(), editStr, String.format("%02d:%02d", hourOfDay, minute));
	//			update();
	//		}
	//	};

	@Override
	protected void initWidget() {
		super.initWidget();
		/***********************
		 * set upbar
		 **********************/
		/* set Title to date */
		tv_title.setText(String.format("%s %d, %d", getResources().getStringArray(R.array.monthName)[g_date.month], g_date.monthDay, g_date.year));

		/* set left button */
		tv_left.setImageDrawable(getResources().getDrawable(R.drawable.back));
		tv_left.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		/* set right button */
		tv_right.setImageDrawable(getResources().getDrawable(R.drawable.addx));
		tv_right.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Time now = new Time();
				now.setToNow();
				Cursor cs = dbHelper.getRecord("date = ? AND time = ?", new String[]{g_date.toDateStr(), now.format("%H:%M")}, null);
				if ( cs.getCount() > 0)
				{
					cs.close();
					return;
				}
				cs.close();
				dbHelper.putRecord(g_date.toDateStr(), now.format("%H:%M"));
				update();
			}
		});

		/* set ListView */
		lv = (ListView) this.findViewById(R.id.listView1);
		lv.setOnItemClickListener(new ItemClickListener());


		/* set Edit Bar */
		editBar = (LinearLayout) this.findViewById(R.id.editBar);
		editBar.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				v.setVisibility(View.INVISIBLE);
			}
		});

		Button b_del = (Button) this.findViewById(R.id.button2);
		b_del.setText(getResources().getString(R.string.str_delete));
		b_del.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				loadAD();
				dbHelper.delRecord(g_date.toDateStr(), editStr);
				editBar.setVisibility(View.INVISIBLE);
				update();
			}
		});

		Button b_edit = (Button) this.findViewById(R.id.button1);
		b_edit.setText(getResources().getString(R.string.str_title_edit));
		b_edit.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				editBar.setVisibility(View.INVISIBLE);
				Intent intent = new Intent(daily_poop.this, edit_poop.class);
				Bundle bd = new Bundle();
				bd.putInt("db_id", editId);
				intent.putExtras(bd);
				startActivity(intent);
			}
		});
	}
}
